6/8/2020

Introduction

We are going to reate a web page presentation using R Markdown that features a plot created with Plotly. We will host our webpage on either GitHub Pages. Our webpage will contain the date that we created the document, and a plot created with Plotly.

Lets move on….

Loading Plotly Package And Diamond Dataset

If you haven’t install the plotly package install it by using the command install.packages("plotly").

library(plotly) # loading the plotly package
# loading the diamond dataset and assign it to variable data
data <- diamonds[sample(nrow(diamonds), 2500), 
                 c("carat", "price", "clarity", "depth")]

Examine the Diamond Dataset

dim(data) # checking the dimensions of the dataset
[1] 2500    4
names(data) # checking the variable names in the dataset
[1] "carat"   "price"   "clarity" "depth"  
summary(data) # examine the descriptive statistics 
     carat            price          clarity        depth      
 Min.   :0.2100   Min.   :  335   SI1    :616   Min.   :55.00  
 1st Qu.:0.4000   1st Qu.:  972   VS2    :582   1st Qu.:61.10  
 Median :0.7100   Median : 2468   SI2    :416   Median :61.80  
 Mean   :0.8067   Mean   : 4003   VS1    :377   Mean   :61.76  
 3rd Qu.:1.0500   3rd Qu.: 5508   VVS2   :224   3rd Qu.:62.50  
 Max.   :4.0100   Max.   :18574   VVS1   :160   Max.   :70.60  
                                  (Other):125                  

Creating 2D and 3D Scatter Plots

# 2D plot
plot2D <- plot_ly(data, x = ~carat, y = ~price, color = ~carat,
        size = ~carat, text = ~paste("Clarity: ", clarity))

# 3D plot
plot3D <- plot_ly(data, x = ~carat, y = ~price, z = ~depth,
        color = ~carat, size = ~carat, 
        text = ~paste("Clarity: ", clarity)) 

2D Plot

# calling the 2D plot
plot2D

3D Plot

# calling the 3D plot
plot3D